class Crd2pulumi < Formula
  desc "Generate typed CustomResources from a Kubernetes CustomResourceDefinition"
  homepage "https://github.com/pulumi/crd2pulumi"
  url "https://github.com/pulumi/crd2pulumi/archive/refs/tags/v1.5.4.tar.gz"
  sha256 "3dfb4e2e7eb9633c156c78becb866836760ee4c87056d0c47a8288cef85dc14c"
  license "Apache-2.0"
  head "https://github.com/pulumi/crd2pulumi.git", branch: "master"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "8170fedbf98664acb53a30c8534c6c879b1726ccf71d11a15a43005e76e3db3e"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "bd3797b91aa02e1f26a4f8a38e9eed076ba077c801eaa258fcc14b5b6c9f5ec3"
    sha256 cellar: :any_skip_relocation, arm64_ventura: "34e1288847de72027899d4fd4ad491a98b1182f1a7d601dec396435dcd79fbaa"
    sha256 cellar: :any_skip_relocation, sonoma:        "e3e04265eaa6c1910e0d05fec10abcdd1a16609965b2ecbf3c31928a78ec87db"
    sha256 cellar: :any_skip_relocation, ventura:       "8d722d5239b46bf83507362e64a913c85c5cbb768d4e40dfc31bfcf59baa0331"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "462512fa6286cb7b402803013123c20beb38cfaf02fec22ee111ed82a6108da8"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "3a6f39e942c24af8ab6577bed69d1ce3c27fdcad9310225d2e248448b22da5ea"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w -X github.com/pulumi/crd2pulumi/cmd.Version=#{version}")

    generate_completions_from_executable(bin/"crd2pulumi", "completion")
  end

  test do
    assert_match version.to_s, shell_output("#{bin}/crd2pulumi version")

    test_crd = testpath/"crd.yaml"
    test_crd.write <<~YAML
      apiVersion: apiextensions.k8s.io/v1
      kind: CustomResourceDefinition
      metadata:
        name: widgets.example.com
      spec:
        group: example.com
        names:
          kind: Widget
          plural: widgets
          singular: widget
        scope: Namespaced
        versions:
          - name: v1
            served: true
            storage: true
            schema:
              openAPIV3Schema:
                type: object
                properties:
                  spec:
                    type: object
                    properties:
                      size:
                        type: string
                      color:
                        type: string
    YAML

    # Generate TypeScript code from the CRD
    system bin/"crd2pulumi", "--nodejsPath", testpath/"typescript", test_crd, "--force"

    assert_path_exists testpath/"typescript/example/v1/widget.ts"
    assert_match "this file was generated by crd2pulumi", (testpath/"typescript/provider.ts").read

    # Generate Python code from the CRD
    system bin/"crd2pulumi", "--pythonPath", testpath/"python", "--python", test_crd
    assert_path_exists testpath/"python/pulumi_crds/example/v1/Widget.py"
    assert_match "this file was generated by crd2pulumi", (testpath/"python/pulumi_crds/provider.py").read
  end
end
